package lambda;

import org.jetbrains.annotations.Contract;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 函数式编程
 * Predicate函数接口的使用方法
 */
public class PredicateSample {
    public static void main(String[] args) {
        Predicate<Integer> predicate = n -> n > 4;//判断函数接口
        boolean result = predicate.test(10);
//        System.out.println(result);//true
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        filter(list, n -> n % 2 == 1);//所有的奇数
        System.out.println();
        filter(list, n -> n % 2 == 0);//所有的偶数
        System.out.println();
        filter(list, n -> n % 2 == 0 && n > 5);//大于5的偶数
    }

    @Contract(pure = true)
    public static void filter(List<Integer> list, Predicate<Integer> predicate) {
        for (Integer num : list) {
            if (predicate.test(num)) {
                System.out.print(num + " ");
            }
        }
    }
}
